/*
 * Copyright 2010, 2011 Matt Oliveri
 *
 * This file is part of JConfigGen
 *
 * JConfigGen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JConfigGen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JConfigGen.  If not, see <http://www.gnu.org/licenses/>.
 */

package atma.jconfiggen;

import java.io.PrintStream;
import java.util.List;
import java.util.Collections;

/** Array creation expression.
 * <p>
 * A {@code NewArray} expression is evaluated by first evaluating the initial elements in order (if applicable), then performing the array creation.
 * The result of a {@code NewArray} is the newly created array.
 * A {@code NewArray} expression can be optionally assigned an id.
 * </p>
 */
public final class NewArray extends TupleExpr
{
	private final String type;
	private final String length;
	private final String elemstr;

	/** Construct a {@code NewArray} with a {@code List<Expr>} for initial array elements.
	 * @param c The {@link Config} that will contain this expression.
	 * @param t The result type of this expression, or null.
	 * @param i The id to assign to this expression, or null.
	 * @param e A list of subexpressions of the {@code NewArray} to use as initial array elements. Can be empty, but not null.
	 */
	public NewArray(Config c,String t,String i,List<Expr> e)
	{
		super(c,i,e);
		type = t;
		length = null;
		elemstr = null;
	}

	/** Construct a {@code NewArray} with a Java expression {@link String} specifying the length of the array.
	 * This form is like Java's <code>new <i>type</i>[<i>length</i>]</code> form instead of the <code>new <i>type</i>[] {<i>elems</i>}</code> form.
	 * @param c The {@link Config} that will contain this expression.
	 * @param t The result type of this expression, or null.
	 * @param i The id to assign to this expression, or null.
	 * @param l A Java expression specifying the length of the array to create. The expression should evaluate to an {@code int}.
	 * @param d Dummy parameter to distinguish the constructor overloads. Ignored.
	 */
	public NewArray(Config c,String t,String i,String l,int d)
	{
		super(c,i,Collections.<Expr>emptyList());
		type = t;
		length = l;
		elemstr = null;
	}

	/** Construct a {@code NewArray} with a Java array initializer {@link String}.
	 * @param c The {@link Config} that will contain this expression.
	 * @param t The result type of this expression, or null.
	 * @param i The id to assign to this expression, or null.
	 * @param e A Java array initializer, not including curly braces, to initialize the array. E.g.: {@code "false,true,true,false"}, <code>"{1,0},{0,1}"</code>
	 */
	public NewArray(Config c,String t,String i,String e)
	{
		super(c,i,Collections.<Expr>emptyList());
		type = t;
		length = null;
		elemstr = e;
	}

	void printType(PrintStream out) {out.print(type);out.print("[]");}

	void printCore(PrintStream out)
	{
		out.print("new ");
		if (length == null)
		{
			out.print(type);
			out.print("[] {");
			if (elemstr == null) printElems(out);
			else out.print(elemstr);
			out.print('}');
		}
		else
		{
			int dimi = type.indexOf('[');
			if (dimi < 0) out.print(type);
			else out.print(type.substring(0,dimi));
			out.print('[');
			out.print(length);
			out.print(']');
			if (dimi >= 0) out.print(type.substring(dimi));
		}
	}
}
